Q6: Conway’s Game of Life
Exercise: Code up Conway’s Game of Life using numpy
The Game of Life is a cellular automaton devised by mathematician John Horton Conway in 1970. It is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and observing how it evolves. It is Turing complete and can simulate a universal constructor or any other Turing machine.
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
The Game of Life is really (really, really) cool.
There are just four extremely simple rules, and these result in an immense richness of behaviour and complexity.
Any live cell with fewer than two live neighbours dies, as if by underpopulation.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overpopulation.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
https://www.youtube.com/watch?v=C2vgICfQawE&t=221s&ab_channel=RationalAnimations
https://www.youtube.com/watch?v=jvSp6VHt_Pc&ab_channel=TheDevDoctor
https://www.youtube.com/watch?v=xP5-iIeKXE8&ab_channel=PhillipBradbury
Here some web apps to play:
Some computational hints:
https://blog.datawrapper.de/game-of-life/
[2]:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
from BicoccaCoursePython2024 import seconda_lezione as sl
life=sl.Game_life()
def update(frame):
"""
Update function for the animation of the Game of Life.
:param frame: The current frame number (not used directly in this case).
:return: The grid array for updating the animation.
"""
life.next_gen()
mat.set_array(life.grid)
return [mat]
fig, ax = plt.subplots()
mat = ax.matshow(life.grid, cmap="gray")
ax.axis("off")
ani = FuncAnimation(fig, update, frames=1000, interval=20, blit=True)
vid=HTML(ani.to_jshtml())
Video
[4]:
vid
[4]: